<?php
declare (strict_types = 1);

namespace app\admin\controller;


use app\BaseController;
use app\lib\exception\BaseException;
use app\model\Rolltoke;
use app\model\Warehouse;
use think\Exception;
use think\facade\Db;

class Rolltokes extends BaseController
{
    public function index($limit=10)
    {
        $name = request()->param('name');
        $status = request()->param('status',1);
        $time = request()->param('time');
        $school = request()->param('school_id');
        $data=Rolltoke::with(['school','toschool'])->where('status',$status)->where('school_id',$school)
            ->when($name, function ($query) use ($name) {
                $query->where('number', 'like', "%$name%");
            })->when($status, function ($query) use ($status) {
                $query->where('status', $status);
            })->when($time, function ($query) use ($time) {
                $query->whereBetween('create_time', $time);
            })->order('id','desc')->paginate($limit);

        return $this->success($data);
    }

    public function create()
    {
        $params=request()->param();

        Db::startTrans();
        try {
            if(empty($params['product'])){
                throw new BaseException(['msg'=>'請至少添加一條產品!']);
            }else{
                $product=json_decode($params['product'],true);
                foreach ($product as $item){
                    $warehouse = Warehouse::where('school_id', $params['school_id'])->where('product_id', $item['id'])->find();
                    if(!$warehouse){
                        throw new Exception('本學院貨倉沒有' . $item['name'] . '該產品,請前往添加後再操作！');
                    }else{
                        if($warehouse->num>=$item['num']){
                            $number =$warehouse->num - $item['num']  ;
                            $warehouse::where('id', $warehouse->id)->update(['num' => $number, 'points_num' => $number]);

                            $towarehouse = Warehouse::where('school_id', $params['toid'])->where('product_id', $item['id'])->find();
                            if($towarehouse){
                                $number =$towarehouse->num + $item['num']  ;
                                $warehouse::where('id', $towarehouse->id)->update(['num' => $number, 'points_num' => $number]);
                            }else{
                                $warehouse::create([
                                    'school_id'=>$params['toid'],
                                    'product_id'=>$item['id'],
                                    'num'=>$item['num'],
                                    'points_num'=>$item['num'],
                                ]);
                            }
                        }else{
                            throw new Exception('產品：' . $item['name'] . '庫存不足，請調整庫存后再進行操作！');
                        }
                    }
                }
                Rolltoke::create([
                    'school_id'=>$params['school_id'],
                    'toid'=>$params['toid'],
                    'number'=>getNumber('轉倉'),
                    'remark'=>$params['remark'] ??'',
                    'product'=>$params['product']
                ]);
                Db::commit();
                return $this->success();
            }
        }catch (\Exception $e){
            Db::rollback();
            throw new BaseException(['msg'=>$e->getMessage()]);
        }


    }

    public function update($id)
    {
        $params=request()->param();

        Db::startTrans();
        try {
            if(empty($params['product'])){
                throw new BaseException(['msg'=>'請至少添加一條產品!']);
            }else{
                $product=json_decode($params['product'],true);
                foreach ($product as $item){
                    $warehouse = Warehouse::where('school_id', $params['school_id'])->where('product_id', $item['id'])->find();
                    if(!$warehouse){
                        throw new Exception('本學院貨倉沒有' . $item['name'] . '該產品,請前往添加後再操作！');
                    }else{
                        if($warehouse->num>=$item['num']){
                            $number =$warehouse->num - $item['num'] ;
                            $warehouse::where('id', $warehouse->id)->update(['num' => $number, 'points_num' => $number]);

                            $towarehouse = Warehouse::where('school_id', $params['toid'])->where('product_id', $item['id'])->find();
                            if($towarehouse){
                                $number =$warehouse->num + $item['num']  ;
                                $warehouse::where('id', $towarehouse->id)->update(['num' => $number, 'points_num' => $number]);
                            }else{
                                $warehouse::create([
                                    'school_id'=>$params['toid'],
                                    'product_id'=>$item['id'],
                                    'num'=>$item['num'],
                                    'points_num'=>$item['num'],
                                ]);
                            }
                        }else{
                            throw new Exception('產品：' . $item['name'] . '庫存不足，請調整庫存后再進行操作！');
                        }
                    }
                }
                Rolltoke::update([
                    'school_id'=>$params['school_id'],
                    'toid'=>$params['toid'],
                    'remark'=>$params['remark'] ??'',
                    'product'=>$params['product']
                ],['id'=>$id]);
                Db::commit();
                return $this->success();
            }
        }catch (\Exception $e){
            Db::rollback();
            throw new BaseException(['msg'=>$e->getMessage()]);
        }
    }

    public function delete($id)
    {
        Rolltoke::where('id','in',$id)->update(['status'=>-1]);
        return $this->success();
    }
}
